<?php
/**
* TopSites - Компонент каталога ссылок для Joostina CMS.
* @author:      Лаборатория сайтов, http://sitelab.net.ru
* @version:     1.9
* @release:     2010.02.07
* @package:     com_topsites
* @copyright:   (C) 2010 http://sitelab.net.ru,  see /administrator/components/com_topsites/copyright.php
* @license:     http://www.gnu.org/copyleft/gpl.html GNU/GPL
**/

defined( '_VALID_MOS' ) or die( '' );


class topsites extends mosDBTable 
{
   var $id = null;
   var $catid = null;
   var $name = null;
   var $url = null;
   var $url_back = null;
   var $description = null;
   var $date = null;
   var $bannercode = null;
   var $image = null;
   var $metakey = null;
   var $metadesc = null;
   var $expiredate = null;
   var $published = null;
   var $uhit = null;
   var $userid = null;
   var $email = null;
   var $controlofid = null;
   var $contact = null;
   
   function topsites(&$db)
   {
      $this->mosDBTable('#__topsites', 'id', $db);
   }
    
   function check()
   {
	// если нет протокала - добавим
   	if (!strstr($this->url,"://")) $this->url="http://".$this->url;
   	if (!strstr($this->url_back,"://")) $this->url_back="http://".$this->url_back;

        // проверим есть ли уже такая ссылка в базе
	$query = "SELECT id"
	  . "\n FROM #__topsites"
	  . "\n WHERE url = '$this->url'"
	  ;
	  $this->_db->setQuery( $query );
	  $xid = intval( $this->_db->loadResult() );
	
	if ($xid && $xid != intval( $this->id )) {
	     $this->_error = TL_LINK_EXIST;
        return false;
	  }
   
   return true;
   }	
   	  
   function getCatID() {
	$this->_db->setQuery( "SELECT catid FROM #__topsites WHERE id = $this->id LIMIT 1" );
	return $this->_db->loadResult();
   }

   function getCatName() {
	global $_MT_LANG;

	$this->_db->setQuery( "SELECT c.cat_id, cat_name FROM #__mt_cl AS cl, #__mt_cats AS c WHERE cl.cat_id = c.cat_id AND link_id = $this->link_id AND main = 1 LIMIT 1" );
	$this->_db->loadObject( $cat );

	if ( !isset($cat->cat_id) || $cat->cat_id == 0 ) 
	     { return $_MT_LANG->ROOT;	} 
	else { return $cat->cat_name;	}
   }

   
}

class topsitesCategories extends mosDBTable 
{
   var $id = null;
   var $name = null;
   var $cat_parent = null;
   var $image = null;
   var $description = null;
   var $published = null;
   var $access = null; 
   var $ordering = null; 
   
   function topsitesCategories(&$db)
   {
      $this->mosDBTable('#__topsites_categories', 'id', $db);
   } 
}
			
	
class topsitesText {

   function replaceJavaScript($text) {
	$text = preg_replace( "/javascript/i" , "j&#097;v&#097;script", $text );
	$text = preg_replace( "/alert/i"      , "&#097;lert"          , $text );
	$text = preg_replace( "/about:/i"     , "&#097;bout:"         , $text );
	$text = preg_replace( "/onmouseover/i", "&#111;nmouseover"    , $text );
	$text = preg_replace( "/onmouseout/i" , "&#111;nmouseout"     , $text );
	$text = preg_replace( "/onclick/i"    , "&#111;nclick"        , $text );
	$text = preg_replace( "/onload/i"     , "&#111;nload"         , $text );
	$text = preg_replace( "/onsubmit/i"   , "&#111;nsubmit"       , $text );
	return $text;
   }

   function clearLinks($text) {
	return preg_replace( _JC_REGEXP_LINK, '', $text);
   }

   function cleanText( $text, $maxlength = 0 ) {
   
	$text = str_replace('<br />', ' ', $text );
	$text = trim( preg_replace( '/(\s){2,}/i', '\\1', $text ) );
	$text = mosHTML::cleanText( $text );
	$text = html_entity_decode( $text );

	if ($maxlength && strlen( $text ) > $maxlength ) {
		$text = substr( $text, 0, $maxlength ) . '...';
	}
	return $text;
   }
	
}	


?>
